package dp.gr03.payroll.application.commands;

import java.util.GregorianCalendar;

import dp.gr03.payroll.application.DateParser;
import dp.gr03.payroll.exception.InvalidArgumentsException;
import dp.gr03.payroll.exception.PayRollException;
import dp.gr03.payroll.payrollApplication.Transaction;
import dp.gr03.payroll.transactionFactory.TransactionFactoryImplementation;

public class TimeCardCommand implements TransactionParser
{

    @Override
    public String getActionString()
    {
        return "TimeCard";
    }

    @Override
    public Transaction parse(String[] arguments) throws PayRollException
    {
        if (arguments.length == 4)
        {
            int employeeId = 0;
            double hours = 0;
            GregorianCalendar date = null;
            try
            {
                employeeId = Integer.parseInt(arguments[1]);
                date = DateParser.parseDate(arguments[2]);
                hours = Double.parseDouble(arguments[3]);
            } catch (NumberFormatException e)
            {
                throw new PayRollException("The employee ID and hours should be numbers.");
            }
            return TransactionFactoryImplementation.getInstance().makeTimecardTransaction(date, hours, employeeId);
        } else
        {
            throw new InvalidArgumentsException();
        }
    }

}
